<script setup lang="ts">
	import {ref,reactive,getCurrentInstance} from 'vue'
	import {shortOrderStatus} from '@/utlis/common.js'
	import {
		onShow,
		onHide,
		onLoad,
		onReachBottom
	} from '@dcloudio/uni-app'
	const {proxy} = getCurrentInstance()
	import {
			useRouter
		} from 'uni-mini-router'
	const router = useRouter()

	const state = reactive({
		orderId:'',
		type:'',
		info:{},
		radiolist:[],
		params:{}
	})
	
	const showPopup = ref(false)
	
	
	const setShortOrderStatus = (val)=>{
		let idx =shortOrderStatus.findIndex(el=>el.value == val)
		let text = ''
		if(idx!=-1){
			text = shortOrderStatus[idx].name
		}else{
			text = ''
		}
		return text;
	}
	
	const copy = () => {
		uni.setClipboardData({
			data:state.info.orderno,
			success: () => {
				uni.showToast({
					icon: 'none',
					title: '复制成功'
				});
			}
		});
	}
	const radioChange = (n) => {
		state.params.reasonId = n
		console.log('radioChange', n);
	};
	const cancelOrder = () => {
		showPopup.value = true
		console.log(state.showPopup,'state.showPopup')
	}
	
	//获取详情
	const meiyeShortWordersOrderGetOrderDetailFun = ()=>{
		proxy.$req('GET', proxy.$api.meiyeShortWordersOrderGetOrderDetail,{orderId:state.orderId}).then((res) => {
			if(res.code == 200){
				console.log(res,res.result)
				state.info = res.result
				state.params.orderId = state.info.id
				state.params.userTimePeriodId = state.info.period
				
				console.log(state.info,'state.info')
			}	
		})
	}
	onLoad((option)=>{
		console.log(option,'optionoption')
		state.type = option.type
		state.orderId = option.orderId
		meiyeShortWordersOrderGetOrderDetailFun()
	})
	
	
	// 短时工用户订单取消说明
	const meiyeShortWorkerCancelListFun = ()=>{
		proxy.$req('GET', proxy.$api.meiyeShortWorkerCancelList,{pageNo:1,pageSize:20}).then((res) => {
			if(res.code == 200){
				state.radiolist = res.result.records
			}	
		})
	}
	meiyeShortWorkerCancelListFun()
		
	// 用户取消订单
	const meiyeShortWordersOrderCancelOrderFun = ()=>{
		state.params.operationType = 4
		proxy.$req('PUT', proxy.$api.meiyeShortWordersOrderCancelOrder,state.params).then((res) => {
			if(res.code == 200){
				showPopup.value = false
				uni.showToast({
					icon: 'none',
					title: '操作成功'
				});
				router.push({
					name: 'shortOrder'
				})
			}else{
				uni.showToast({
					icon: 'none',
					title: res.message
				});
			}		
		})
	}
	
	// 用户接单
	const meiyeShortWordersOrderUserAcceptOrderFun = ()=>{
		state.params.operationType = 1
		proxy.$req('PUT', proxy.$api.meiyeShortWordersOrderUserAcceptOrder,state.params).then((res) => {
			if(res.code == 200){
				uni.showToast({
					icon: 'none',
					title: '操作成功'
				});
				router.push({
					name: 'shortOrder'
				})
			}else{
				uni.showToast({
					icon: 'none',
					title: res.message
				});
			}	
		})
	}
	
	// 用户完成服务
	const meiyeShortWordersOrderUserCompleteServiceFun = ()=>{
		state.params.operationType = 2
		proxy.$req('PUT', proxy.$api.meiyeShortWordersOrderUserCompleteService,state.params).then((res) => {
			if(res.code == 200){
				uni.showToast({
					icon: 'none',
					title: '操作成功'
				});
				router.push({
					name: 'shortOrder'
				})
			}else{
				uni.showToast({
					icon: 'none',
					title: res.message
				});
			}		
		})
	}
	const cancelSubmit = ()=>{
		if(state.params.reasonId == '' || state.params.reasonId == undefined){
			uni.showToast({
				icon: 'none',
				title: '请选择取消原因'
			});
			return
		}
		meiyeShortWordersOrderCancelOrderFun()
	}
	// 用户接单
	const orderReceiving = () => {
		meiyeShortWordersOrderUserAcceptOrderFun()
	}
	// 用户完成服务
	const subOrder = () => {
		meiyeShortWordersOrderUserCompleteServiceFun()
	}
	const close = ()=>{
		showPopup.value = false
	}
	const open = ()=>{
		
	}
</script>

<template>
	<u-navbar class="navbar"  bgColor="rgba(255, 255, 255, 0)" :placeholder="true" title="" :autoBack="true">
		<template #center>
			<view style="font-weight: 600;">订单详情</view>
		</template>
	</u-navbar>
	<view class="mainBox">
		<view class="mianBoxH">
			<view class="headL">
				{{state.info.employmentType==0?`日雇佣·${state.info.num}日`:`小时雇佣·${state.info.num}小时`}}
		<!-- 		{{state.info.employmentType == 1?'时雇佣':'日雇佣'}}
				{{state.info.num}} -->
			</view>
			<view class="headR">
				{{setShortOrderStatus(state.info.status)}}
			</view>
		</view>
		<view class="mainC">
			<view class="line">
				<view class="lineL">
					雇佣类型
				</view>
				<text class="lineR">{{state.info.employmentType==0?"按日":'按小时'}}</text>
			</view>
			<view class="line">
				<view class="lineL" v-if="state.info.employeeDateTime">
					雇佣时间
				</view>
				<text class="lineR" v-if="state.info.employmentType == 1">{{state.info.employeeDateTime.workTime}}</text>
				<text class="lineR" v-else>{{state.info.employeeDateTime?state.info.employeeDateTime.workDate:''}}</text>
			</view>
			<view class="line">
				<view class="lineL">
					雇 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;主
				</view>
				<text class="lineR">{{state.info.employerName}}</text>
			</view>
			<view class="line">
				<view class="lineL">
					工作地点
				</view>
				<text class="lineR">{{state.info.address}}</text>
			</view>
			<view class="lineEnd">
				<view class="endR">
					<text class="xinzi">薪资</text><text class="yuan">￥</text><text class="num intFont">{{state.info.salary}}</text>
				</view>
			</view>
		</view>
		<view class="afterSales" v-if="state.info.refundApplyTime">
			<view class="line">
				<view class="lineL">
					退款原因
				</view>
				<view class="lineR">
					{{state.info.reasonRefund}}
				</view>
			</view>
			<view class="line">
				<view class="lineL">
					退还金额
				</view>
				<view class="lineR intFont">
					￥{{state.info.refundAmount}}
				</view>
			</view>
			<view class="line">
				<view class="lineL">
					申请时间
				</view>
				<view class="lineR">
					{{state.info.refundApplyTime}}
				</view>
			</view>
			<view class="line">
				<view class="lineL">
					退款时间
				</view>
				<view class="lineR">
					{{state.info.refundTime}}
				</view>
			</view>
		</view>
		<view class="mainFoot">
			<view class="line">
				<view class="lineL">
					订单编号
				</view>
				<view class="">
					<text class="lineR">{{state.info.orderno}}</text>
					<text class="copy" @click="copy">复制</text>
				</view>
			</view>
			<view class="line">
				<view class="lineL">
					下单时间
				</view>
				<view class="lineR">
					{{state.info.orderTime}}
				</view>
			</view>
			<view class="line">
				<view class="lineL">
					支付时间
				</view>
				<view class="lineR">
					{{state.info.payTime}}
				</view>
			</view>
		</view>
		
		
		<view class="subBtn">
			<view class="cancelOrder btn" @click="cancelOrder" v-if="state.info.status =='0' || state.info.status =='1'">
				<view class="text" >
					取消订单
				</view>
			</view>
			<view class="subOrder btn" @click="orderReceiving" v-if="state.info.status =='0'">
				<view class="text">
					接单
				</view>
			</view>
			<view class="subOrder btn" @click="subOrder" v-if="state.info.status =='1'">
				<view class="text">
					完成订单
				</view>
			</view>
		</view>	
		
		<u-popup :show="showPopup" :round="10" @close="close" @open="open">
			<view class="popupMain">
				<view class="title">选择取消原因</view>
				<u-radio-group placement="column">
				    <u-radio
						:customStyle="{marginBottom: '18px'}"
						v-for="item in state.radiolist"
						:key="item.id"
						:label="item.cancelContent"
						:name="item.id"
						@change="radioChange"
				    >
				    </u-radio>
				</u-radio-group>
				<view class="popupBtn" @click="cancelSubmit">
					<text>确认取消</text>
					<text>服务开始24小时内不可取消</text>
				</view>
			</view>
			
		</u-popup>
		
	</view>
</template>

<style lang="scss" scoped>
	page{
		background: #FAFAFA;
	}
	.mainBox{
		width: 686rpx;
		height: 990rpx;
		background: #FFFFFF;
		box-shadow: 8rpx 8rpx 32rpx 0rpx rgba(0,0,0,0.04);
		border: 2rpx solid #FFFFFF;
		margin:20rpx auto;
		border-radius: 20rpx;
		padding: 20rpx 10rpx;
		box-sizing: border-box;
		.afterSales{
			border-bottom:1rpx solid rgba(0,0,0,0.05);
			padding: 20rpx 20rpx;
			box-sizing: border-box;
			.line{
				display: flex;
				justify-content: space-between;
				margin: 15rpx 0;
				.lineL{
					font-size: 28rpx;
					font-weight: 600;
				}
				.lineR{
					font-size: 24rpx;
					font-weight: 400;
					color: rgba(0,0,0,0.65);
					line-height: 34rpx;
				}
			}
		}
		.mainFoot{
			margin-top: 20rpx;
			padding: 20rpx 20rpx 0 20rpx;
			box-sizing: border-box;
			.line{
				display: flex;
				justify-content: space-between;
				margin: 15rpx 0;
				.copy{
					width: 48rpx;
					height: 34rpx;
					font-size: 24rpx;
					font-weight: 400;
					color: black;
					border-left: 1rpx solid #ccc;
					margin-left: 5rpx;
					padding-left: 5rpx;
				}
				.lineL{
					font-size: 28rpx;
					font-weight: 600;
				}
				.lineR{
					font-size: 24rpx;
					font-weight: 400;
					color: rgba(0,0,0,0.65);
					line-height: 34rpx;
				}
			}
		}
		.mainC{
			margin-top: 20rpx;
			border-bottom: 1rpx solid rgba(0,0,0,0.05);
			padding-bottom: 70rpx;
			.lineEnd{
				width: 650rpx;
				margin: 0 auto;
				padding: 10rpx 20rpx;
				box-sizing: border-box;
				.endR{
					float: right;
					.xinzi{
						display: inline-block;
						font-size: 32rpx;
						font-weight: 600;
						margin-right: 10rpx;
					}
					.yuan{
						font-size: 28rpx;
						font-weight: bold;
						color: #F22F19;
					}
					.num{
						font-size: 44rpx;
						font-weight: bold;
						color: #F22F19;
						line-height: 52rpx;
					}
				}
			}
			.line{
				width: 650rpx;
				height: 64rpx;
				padding: 0 20rpx;
				box-sizing: border-box;
				display: flex;
				justify-content: space-between;
				.lineL{
					font-size: 28rpx;
					font-weight: 600;
					
				}
				.lineR{
					width: 460rpx;
					color: #3b3b3b;
					font-size: 28rpx;
					font-weight: 400;
					
				}
			}
		}
		.mianBoxH{
			height: 80rpx;
			padding: 10rpx 10rpx;
			box-sizing: border-box;
			display: flex;
			margin: 0 auto;
			justify-content: space-between;
			width: 654rpx;
			border-bottom: 1rpx solid rgba(0,0,0,0.05);
			.headL{
				font-size: 32rpx;
				font-weight: 600;
				color: #000000;
			}
			.headR{
				font-size: 28rpx;
				font-weight: 400;
				color: #F22F19;
			}
		}
		
		.subBtn{
			bottom: 0;
			position: absolute;
			left:0;
			width: 100%;
			height: 145rpx;
			display: flex;
			justify-content: flex-end;
			flex-direction: row;
			flex-wrap: nowrap;
			padding-top: 20rpx;
			border-top: 1px solid #D2D2D2;
			.btn{
				width: 159rpx;
				height: 59rpx;
				line-height: 59rpx;
				text-align: center;
				margin: 0 10rpx;
				border-radius: 8rpx;
			}
			.cancelOrder{
				border: 1rpx solid #D2D2D2;
			}
			.subOrder{
				border: 1rpx solid #F83D3D;
				.text{
					color: #F83D3D;
				}
			}
		}
	}
	
	.popupMain{
		padding:20rpx;
		.title{
			padding:40rpx 18rpx;
		}
		.popupBtn{
			display:flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			background:#CB645E;
			width:80%;
			padding:8rpx 0;
			margin:20rpx auto;
			color:#fff;
			border-radius: 16rpx;
			text:nth-child(1){
				font-size:28rpx;
				
			}
			text:nth-child(2){
				font-size:24rpx;
				
			}
			
		}
	}
</style>
